package com.yb.socket.udp

import android.app.ProgressDialog
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import com.blankj.utilcode.util.ToastUtils
import com.yb.socket.R
import com.yb.socket.databinding.FragmentUdpBinding

private const val CLIENT_PORT = 8886
private const val SERVICE_PORT = 8887

class UDPFragment : Fragment() {

    private var serviceIP: String? = null
    private var clientIP: String? = null
    lateinit var binding: FragmentUdpBinding


    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_udp, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding.btnClient.setOnClickListener { initClient() }
        binding.btnServer.setOnClickListener { initServer() }
    }

    private fun initServer() {
        binding.linearType.visibility = View.GONE
        binding.linear.visibility = View.VISIBLE

        binding.connect.visibility = View.GONE
        UDPUtils.receiveMessage(SERVICE_PORT, true, null) { ip, data ->
            this.clientIP = ip
            binding.tvLogger.logSuccess("接收到($ip)客户端消息：$data")
        }
        binding.sendMessage.setOnClickListener { v ->
            if (clientIP == null) return@setOnClickListener ToastUtils.showShort("没客户端")
            val data: String = binding.edClientMessage.text.toString()
            UDPUtils.sendMessage(clientIP, CLIENT_PORT, data, null)
            binding.tvLogger.logNormal("发送到${clientIP}客户端消息：" + data)

        }
    }

    private fun initClient() {
        binding.linearType.visibility = View.GONE
        binding.linear.visibility = View.VISIBLE

        UDPUtils.receiveMessage(CLIENT_PORT, true, null) { ip, data ->
            binding.tvLogger.logSuccess("接收到($ip)服务端消息：$data")
        }
        binding.connect.setOnClickListener {
            val progressDialog = ProgressDialog.show(context, "", "正在连接");

            UDPUtils.sendBroadcast(SERVICE_PORT, "client broadcast", 1000) { ip, data ->
                progressDialog.dismiss()
                this.serviceIP = ip
                binding.tvLogger.logWarning("连接到($ip)服务端：$data")
                UDPUtils.stopSendBroadcast()
            }
        }

        binding.sendMessage.setOnClickListener {
            if (serviceIP == null) return@setOnClickListener ToastUtils.showShort("没建立连接")
            val data = binding.edClientMessage.text.toString()
            UDPUtils.sendMessage(serviceIP, SERVICE_PORT, data, null)
            binding.tvLogger.logNormal("发送到($serviceIP)服务端消息：$data")
        }


    }

    companion object {

        @JvmStatic
        fun newInstance() = UDPFragment()
    }
}